﻿using System;
using System.Web;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;


/// <summary>
/// DBAi 的摘要说明。
/// </summary>
public class DBAi
{
    public static string strConnection;

    public DBAi()
    {
        DBAi.strConnection = ConfigurationManager.AppSettings["ConnString"];

    }
    public static string getstrConnection()
    {
        return ConfigurationManager.AppSettings["ConnString"];
    }
    public static int ExeSqlCommand(string sqlCommand)
    {
        SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection());
        SqlCommand command1 = new SqlCommand(sqlCommand, connection1);
        int num1 = -1;
        try
        {
            connection1.Open();
        }
        catch
        {
            return -1;
        }
        try
        {
            num1 = command1.ExecuteNonQuery();
        }
        catch (Exception exception1)
        {
            //WebFun.ScriptMessage("SQL错误:" + exception1.Message, true);
            //Common.WebFun.ClientScriptBack2("alert('SQL错误:" + exception1.Message+"');", false);
        }
        connection1.Close();
        return num1;
    }
    public static int ExeSqlCommand(string sqlCommand, SqlParameter[] prams)
    {
        using (SqlConnection conn = new SqlConnection(DBAi.getstrConnection()))
        {
            using (SqlCommand command1 = SqlDBA.CreateCommandSql(conn, sqlCommand, prams))
            {
                int num1 = -1;
                try
                {
                    conn.Open();
                }
                catch
                {
                    return -1;
                }
                try
                {
                    num1 = command1.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    //logo.ErrLog("DBA数据层_错误", ex, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                    //Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand);
                }
                command1.Dispose();
                conn.Close();
                conn.Dispose();
                return num1;
            }
        }

    }
    public static int ExeSqlCommand(string sqlCommand, ref Exception exception)
    {
        SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection());
        SqlCommand command1 = new SqlCommand(sqlCommand, connection1);
        try
        {
            connection1.Open();
        }
        catch (Exception exception1)
        {
            exception = exception1;
            return -1;
        }
        int num1 = command1.ExecuteNonQuery();
        connection1.Close();
        return num1;
    }

    public static bool ExeSqlCommands(string[] sqlCommands)
    {
        if (sqlCommands.Length < 1)
        {
            return false;
        }
        SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection());
        try
        {
            connection1.Open();
        }
        catch
        {
            return false;
        }
        int num1 = 0;
        using (SqlTransaction transaction1 = connection1.BeginTransaction())
        {
            while (num1 < sqlCommands.Length)
            {
                SqlCommand command1 = new SqlCommand(sqlCommands[num1], connection1);
                command1.Transaction = transaction1;
                try
                {
                    command1.ExecuteNonQuery();
                }
                catch
                {
                    transaction1.Rollback();
                    return false;
                }
                num1++;
            }
            if (num1 >= sqlCommands.Length)
            {
                transaction1.Commit();
            }
        }
        if (connection1.State != ConnectionState.Closed)
        {
            connection1.Close();
        }
        return true;
    }

    public static int ExeSqlCommands(string[] sqlCommands, bool mustExist)
    {
        int num1 = 0;
        if (sqlCommands.Length >= 1)
        {
            SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection());
            try
            {
                connection1.Open();
            }
            catch
            {
                return -1;
            }
            int num2 = 0;
            int num3 = 0;
            using (SqlTransaction transaction1 = connection1.BeginTransaction())
            {
                while (num2 < sqlCommands.Length)
                {
                    SqlCommand command1 = new SqlCommand(sqlCommands[num2], connection1);
                    command1.Transaction = transaction1;
                    try
                    {
                        num3 = command1.ExecuteNonQuery();
                    }
                    catch
                    {
                        num3 = -1;
                    }
                    if ((num3 < 0) || (mustExist && (num3 == 0)))
                    {
                        num1 = -2;
                        transaction1.Rollback();
                        break;
                    }
                    num1 += num3;
                    num2++;
                }
                if (num2 >= sqlCommands.Length)
                {
                    transaction1.Commit();
                }
            }
            if (connection1.State != ConnectionState.Closed)
            {
                connection1.Close();
            }
        }
        return num1;
    }

    public static int ExeSqlCommands(ArrayList sqlCommands, bool mustExist)
    {
        int num1 = 0;
        if (sqlCommands.Count >= 1)
        {
            SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection());
            try
            {
                connection1.Open();
            }
            catch
            {
                return -1;
            }
            int num2 = 0;
            int num3 = 0;
            using (SqlTransaction transaction1 = connection1.BeginTransaction())
            {
                while (num2 < sqlCommands.Count)
                {
                    SqlCommand command1 = new SqlCommand(sqlCommands[num2].ToString(), connection1);
                    command1.Transaction = transaction1;
                    try
                    {
                        num3 = command1.ExecuteNonQuery();
                    }
                    catch
                    {
                        num3 = -1;
                    }
                    if ((num3 < 0) || (mustExist && (num3 == 0)))
                    {
                        num1 = -2;
                        transaction1.Rollback();
                        break;
                    }
                    num1 += num3;
                    num2++;
                }
                if (num2 >= sqlCommands.Count)
                {
                    transaction1.Commit();
                }
            }
            if (connection1.State != ConnectionState.Closed)
            {
                connection1.Close();
            }
        }
        return num1;
    }

    public static DataTable GetDBToDataTable(string sqlCommand)
    {
        SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection());
        SqlDataAdapter adapter1 = new SqlDataAdapter();
        adapter1.SelectCommand = new SqlCommand(sqlCommand, connection1);
        try
        {
            connection1.Open();
        }
        catch
        {
            return null;
        }
        DataTable table1 = new DataTable();
        try
        {
            adapter1.Fill(table1);
        }
        catch (Exception exception1)
        {
            //WebFun.ScriptMessage("SQL错误:" + exception1.Message, true);
            //return null;
        }
        adapter1.Dispose();
        connection1.Close();
        connection1.Dispose();
        //WebFun.ScriptMessage("SQL错误:" + connection1.State.ToString(), true);
        return table1;
    }
    public static DataTable GetDBToDataTable(string sqlCommand, SqlParameter[] prams)
    {
        using (SqlConnection conn = new SqlConnection(DBAi.getstrConnection()))
        {
            using (SqlDataAdapter adapter1 = new SqlDataAdapter())
            {
                using (adapter1.SelectCommand = SqlDBA.CreateCommandSql(conn, sqlCommand, prams))
                {
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        //Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand);
                        return null;
                    }
                    DataTable table1 = new DataTable();
                    try
                    {
                        adapter1.Fill(table1);
                    }
                    catch (Exception ex)
                    {
                        //Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand);
                    }
                    adapter1.Dispose();
                    conn.Close();
                    conn.Dispose();
                    return table1;
                }
            }
        }
        //return SqlDBA.RunProcc(conn, sqlCommand, prams);
    }
    public static DataRowCollection GetDBValue(string sqlCommand)
    {
        return DBAi.GetDBToDataTable(sqlCommand).Rows;
    }

    public static ArrayList GetDBValue_1(string sqlCommand)
    {
        SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection());
        SqlCommand command1 = new SqlCommand(sqlCommand, connection1);
        try
        {
            connection1.Open();
        }
        catch
        {
            return null;
        }
        SqlDataReader reader1 = command1.ExecuteReader();
        if (!reader1.HasRows)
        {
            reader1.Close();
            connection1.Close();
            return null;
        }
        ArrayList list1 = new ArrayList();
        if (reader1.Read())
        {
            for (int num1 = 0; num1 < reader1.FieldCount; num1++)
            {
                list1.Add(reader1[num1]);
            }
        }
        reader1.Close();
        connection1.Close();
        return list1;
    }

    public static ArrayList GetDBValue_2(string sqlCommand)
    {
        SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection());
        SqlCommand command1 = new SqlCommand(sqlCommand, connection1);
        try
        {
            connection1.Open();
        }
        catch
        {
            return null;
        }
        SqlDataReader reader1 = command1.ExecuteReader();
        if (!reader1.HasRows)
        {
            reader1.Close();
            connection1.Close();
            return null;
        }
        ArrayList list1 = new ArrayList();
        while (reader1.Read())
        {
            list1.Add(reader1[0]);
        }
        reader1.Close();
        connection1.Close();
        return list1;
    }

    public static object GetDBValue_3(string sqlCommand, SqlParameter[] prams)
    {
        using (SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection()))
        {
            using (SqlCommand command1 = SqlDBA.CreateCommandSql(connection1, sqlCommand, prams))
            {
                try
                {
                    connection1.Open();
                }
                catch
                {
                    return null;
                }
                object obj1 = command1.ExecuteScalar();
                connection1.Close();
                return obj1;
            }
        }
    }
    public static object GetDBValue_3(string sqlCommand)
    {
        using (SqlConnection connection1 = new SqlConnection(DBAi.getstrConnection()))
        {
            using (SqlCommand command1 = new SqlCommand(sqlCommand, connection1))
            {
                try
                {
                    connection1.Open();
                }
                catch
                {
                    return null;
                }
                object obj1 = command1.ExecuteScalar();
                connection1.Close();
                return obj1;
            }
        }
    }
    public static DataSet GetList(string TBName, int PageSize, int CurPage, string KeyField, string Condition, string Order)
    {
        DataSet dataReader = null;
        SqlConnection conn = new SqlConnection(DBAi.getstrConnection());
        //			SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnString"]);
        try
        {
            SqlParameter[] prams = {
                                           SqlDBA.MakeInParam("@TBName", SqlDbType.NVarChar, 100, TBName),
                                           SqlDBA.MakeInParam("@PageSize", SqlDbType.Int,10, PageSize),
                                           SqlDBA.MakeInParam("@CurPage", SqlDbType.Int, 10, CurPage),
                                           SqlDBA.MakeInParam("@KeyField", SqlDbType.NVarChar, 100, KeyField),
                                           SqlDBA.MakeInParam("@Condition", SqlDbType.NVarChar, 200, Condition),
                                           SqlDBA.MakeInParam("@Order", SqlDbType.NVarChar, 200, Order)
                                       };
            SqlDBA.RunProc(conn, "XW_PublicTurnPageWebSite", prams, out dataReader);
        }
        catch (Exception exception1)
        {
            //WebFun.ScriptMessage("SqlDBA发生错误，信息为：" + exception1.Message, true);
        }
        return dataReader;
    }
    public static DataSet GetNewList(string 表名, int 每页显示数, int 当前页, string 查询条件, string 表的主键, string 排序字段, int 排序方法, out int 总页数, out int 记录数)
    {
        DataSet dataReader = null;
        SqlConnection conn = new SqlConnection(DBAi.getstrConnection());
        //			SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
        总页数 = 0;
        记录数 = 0;
        try
        {

            SqlParameter[] prams = {
                                           SqlDBA.MakeInParam("@tblName", SqlDbType.NVarChar, 200, 表名),
                                           SqlDBA.MakeInParam("@pageSize", SqlDbType.Int,10, 每页显示数),
                                           SqlDBA.MakeInParam("@page", SqlDbType.Int, 10, 当前页),
                                           SqlDBA.MakeInParam("@strCondition", SqlDbType.NVarChar, 1000, 查询条件),
                                           SqlDBA.MakeInParam("@ID", SqlDbType.NVarChar, 150, 表的主键),
                                           SqlDBA.MakeInParam("@fldSort", SqlDbType.NVarChar, 200, 排序字段),
                                           SqlDBA.MakeInParam("@Sort", SqlDbType.Bit, 10, 排序方法),
                                           SqlDBA.MakeOutParam("@pageCount", SqlDbType.Int,4),
                                           SqlDBA.MakeOutParam("@Counts", SqlDbType.Int,4)
                                       };
            SqlDBA.RunProc(conn, "proc_ListPageInt", prams, out dataReader);
            总页数 = Convert.ToInt32(prams[7].Value);
            记录数 = Convert.ToInt32(prams[8].Value);
        }
        catch (Exception exception1)
        {
            //WebFun.ScriptMessage("SqlDBA发生错误，信息为：" + exception1.Message, true);
            throw new ArgumentException("SqlDBA发生错误，信息为：" + exception1.Message);
        }
        return dataReader;
    }
}
